package com.sample.javabean;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;




import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sample.action.form.TiceForm;
import com.sample.jdbc.JdbcUtils;
import com.sample.util.Pager;


/**
 * 体测信息类
 * @author梁桂花
 *
 * 2018-10-27
 */
public class Tice {
	
	private static Log LOG =LogFactory.getLog(Tice.class);
	//定义属性
	private Integer id;
	private String	nname	;
	private String	sex	;
	private String	sno	;
	private String	age	;
	private String	ttime	;
	private String	kg;
	private String	cm	;
	private String	run	;
	private String	jump	;
	private String	sit	;
	private String	tree	;
	
	private JdbcUtils jdbcUtils = new JdbcUtils();

	public Tice(Integer id){
		this.id =id;
	}
	
	
	/**
	 * 参合登记信息列表
	 * @return
	 */
	public List<Tice> findAll(){
		Connection conn=null;
		Statement stmt =null;
		ResultSet rs =null;
		List<Tice> ticeList =new ArrayList<Tice>(20);
		try{
			 conn =jdbcUtils.getConn();
			 stmt =conn.createStatement();
			//构造插入sql
			String sql="select * from t_tice where 1=1";
			 rs =stmt.executeQuery(sql);
			while(rs.next()){
				String nname=rs.getString("nname");
				String sex =rs.getString("sex");
			    String	sno=rs.getString("sno");
			    String	age=rs.getString("age");
			    String	ttime=rs.getString("ttime");
				String	kg	=rs.getString("kg");
				 String	cm=rs.getString("cm");
				 String	run=rs.getString("run");
				 String	jump=rs.getString("jump");	
				 String	sit=rs.getString("sit");
				 String	tree=rs.getString("tree");
				
				 Tice model=new Tice(id,nname,sex,sno,age,ttime,kg,cm,run,jump,sit,tree);
				
				ticeList.add(model);
							}
			
		}catch (Exception e) {
			e.printStackTrace();
			LOG.error("加载体测失败!", e);
		}finally{
			jdbcUtils.close(conn, stmt,rs);
		}
		return ticeList;
		
	}
	
     public void load()throws Exception{
 		Connection conn = null;
 		Statement stmt = null;
 		ResultSet rs =null; 
 		try {
 			conn = jdbcUtils.getConn();
 			stmt = conn.createStatement();
 			
 			String sql = "select * from  t_tice where id='"+this.id+"'";
 		
 			rs=stmt.executeQuery(sql);
 			if(rs.next()){
 				this.id =rs.getInt("id");
 				this.nname =rs.getString("nname");
 				this.sex =rs.getString("sex");
 				this.sno=rs.getString("sno");
 			  	this.age=rs.getString("age");
 			   	this.ttime=rs.getString("ttime");
 				this.kg=rs.getString("kg");
 				this.cm=rs.getString("cm");
 				this.run=rs.getString("run");
 		     	this.jump=rs.getString("jump");	
 		     	this.sit=rs.getString("sit");
 		     	this.tree=rs.getString("tree");	
 				
 			}
 			// 5:閿熸埅鎲嬫嫹閿熸枻鎷锋簮
 		} catch (SQLException e) {
 			e.printStackTrace();
 			throw new SQLException("加载体测信息");
 		} finally {
 			jdbcUtils.close(conn, stmt);
 		}	
 	}
 	public void editTice() {

 		Connection conn = null;
 		Statement stmt = null;
 		try {
 			conn = jdbcUtils.getConn();
 			stmt = conn.createStatement();
 			
 			String sql = "update  t_tice set nname='"+this.nname+"',sex='"+this.sex+"',sno='"+this.sno+"',age='"+this.age+"',ttime='"+this.ttime+"',kg='"+this.kg+"',cm='"+this.cm+"',run='"+this.run+"',jump='"+this.jump+"',sit='"+this.sit+"',tree='"+this.tree+"' where id='"+this.id+"'";
 			System.out.println("编辑体测信息");
 			stmt.executeUpdate(sql);
 			// 5:閿熸埅鎲嬫嫹閿熸枻鎷锋簮
 		} catch (SQLException e) {
 			e.printStackTrace();
 		
 		} finally {
 			jdbcUtils.close(conn, stmt);
 		}

 	}
 	public void delTice() {

 		Connection conn = null;
 		Statement stmt = null;
 		try {
 			conn = jdbcUtils.getConn();
 			stmt = conn.createStatement();
 			// 3:閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓绲奞L
 			String sql = "delete from t_tice  where sno='"+this.sno+"'";
 			// 4:鎵ч敓鏂ゆ嫹SQL閿熸枻鎷烽敓鏂ゆ嫹閿熸埅鏂ゆ嫹閿燂拷
 			System.out.println("删除体测信息");
 			stmt.executeUpdate(sql);
 			// 5:閿熸埅鎲嬫嫹閿熸枻鎷锋簮
 		} catch (SQLException e) {
 			e.printStackTrace();
 		} finally {
 			jdbcUtils.close(conn, stmt);
 		}

 	}
 	public void add(){
 		Connection conn =null;
 		Statement stmt=null;
 		try{
 			
 			 conn =jdbcUtils.getConn();
 		
 			String sql="insert into t_tice(nname,sex,sno,age,ttime,kg,cm,run,jump,sit,tree) values('"+this.nname+"','"+this.sex	+"','"+this.sno+"','"+this.age+"','"+this.ttime+"','"+this.kg+"','"+this.cm+"','"+this.run+"','"+this.jump+"','"+this.sit+"','"+this.tree+"')";
 			
 			 stmt =conn.createStatement();
 			stmt.executeUpdate(sql);
 		}catch (Exception e) {
 			e.printStackTrace();
 			
 		}finally{
 			jdbcUtils.close(conn, stmt);
 			
 		}
 		
 	}
 	public Pager<Tice> list(int page,int size,TiceForm form) {

 		Connection conn = null;
 		Statement stmt = null;
 		ResultSet rs =null;
 		Pager<Tice> results=new Pager<Tice>();
 		results.setPage(page);
 		results.setSize(size);
 		try {
 			conn = jdbcUtils.getConn();
 			stmt = conn.createStatement();
 			int start=(page-1)*size;
 			int limit=size;
 			//閿熸枻鎷疯SQL
 			String selectSql="select * from t_tice where 1=1";
 			if(form!=null&&form.getSno()!=null&&!"".equals(form.getSno())){
 				selectSql +=" and sno like'%"+form.getSno()+"%'";
 			}
 			
 			//缁熼敓鏂ゆ嫹SQL
 			String countSql ="select count(*)as c from ("+selectSql+") as t1";
 			//閿熸枻鎷烽〉SQL
 			 String pageSql=selectSql+" limit "+start+","+limit;
 			 rs= stmt.executeQuery(countSql);
 			 int total=0;
 			 while(rs.next()){
 				 total=rs.getInt("c");
 			 }
 			 results.setTotal(total);
 			 //閿熸枻鎷疯閿熸枻鎷烽〉閿熸枻鎷锋伅
 			 rs= stmt.executeQuery(pageSql);
 			 while(rs.next()){
 				 int id=rs.getInt("id");
 				String nname=rs.getString("nname");
				String sex =rs.getString("sex");
			    String	sno=rs.getString("sno");
			    String	age=rs.getString("age");
			    String	ttime=rs.getString("ttime");
				String	kg	=rs.getString("kg");
				 String	cm=rs.getString("cm");
				 String	run=rs.getString("run");
				 String	jump=rs.getString("jump");
				 String	sit=rs.getString("sit");
				 String	tree=rs.getString("tree");
 				 Tice model=new Tice();
 				  model.setId(id);
 				  model.setNname(nname);
 				  model.setSex(sex);
 				  model.setSno(sno);
 				  model.setAge(age);
 				  model.setTtime(ttime);
 				  model.setKg(kg);
 				  model.setCm(cm);
 				  model.setRun(run);
 				  model.setJump(jump);
 				 model.setSit(sit);
				  model.setTree(tree);
 		
 				  results.getDataSets().add(model);
 			 }
 			 results.init();
 			// 5:閿熸埅鎲嬫嫹閿熸枻鎷锋簮
 		} catch (SQLException e) {
 			e.printStackTrace();
 		} finally {
 			jdbcUtils.close(conn, stmt);
 		}
 		return results;

 	}
 	//定义无参构造函数
 		public Tice(){
 			
 		}

	public Tice( Integer id, String nname, String sex, String sno,String age,
			String ttime,String kg,String cm,String run,String jump,String sit,String tree ) {
		super();
		this.id = id;
		this.nname = nname;
		this.sex=sex;
		this.sno=sno;
		this.age=age;
		this.ttime=ttime;
		this.kg=kg;
	    this.cm=cm;
		this.run=run;
		this.jump=jump;
		this.sit=sit;
		this.tree=tree;
	}


	


	public Integer getId() {
		return id;
	}


	public void setId(Integer id) {
		this.id = id;
	}


	public String getNname() {
		return nname;
	}


	public void setNname(String nname) {
		this.nname = nname;
	}


	public String getSex() {
		return sex;
	}


	public void setSex(String sex) {
		this.sex = sex;
	}


	public String getSno() {
		return sno;
	}


	public void setSno(String sno) {
		this.sno = sno;
	}


	public String getAge() {
		return age;
	}


	public void setAge(String age) {
		this.age = age;
	}


	public String getTtime() {
		return ttime;
	}


	public void setTtime(String ttime) {
		this.ttime = ttime;
	}


	public String getKg() {
		return kg;
	}


	public void setKg(String kg) {
		this.kg = kg;
	}


	public String getCm() {
		return cm;
	}


	public void setCm(String cm) {
		this.cm = cm;
	}


	public String getRun() {
		return run;
	}


	public void setRun(String run) {
		this.run = run;
	}


	public String getJump() {
		return jump;
	}


	public void setJump(String jump) {
		this.jump = jump;
	}


	public String getSit() {
		return sit;
	}


	public void setSit(String sit) {
		this.sit = sit;
	}


	public String getTree() {
		return tree;
	}


	public void setTree(String tree) {
		this.tree = tree;
	}


	public JdbcUtils getJdbcUtils() {
		return jdbcUtils;
	}


	public void setJdbcUtils(JdbcUtils jdbcUtils) {
		this.jdbcUtils = jdbcUtils;
	}


	
}
